package com.nbjtjc.safe.service.emg;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicGoodsMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublicGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmgPlanPublicGoodsService {

    @Autowired
    private EmgPlanPublicGoodsMapper emgPlanPublicGoodsMapper;

    public Result add(EmgPlanPublicGoods emgPlanPublicGoods){
        Result result=new Result();
        if(ObjectUtil.isNull(emgPlanPublicGoods)
                ||ObjectUtil.isNull(emgPlanPublicGoods.getPlanPublicId()))
        {
            result.setFalse("关键字段不能为空");
            return result;
        }
        emgPlanPublicGoodsMapper.insert(emgPlanPublicGoods);
        result.setData(emgPlanPublicGoods);
        return result;
    }

    public Result delete(Integer goodsId){
        Result result=new Result();
        if(ObjectUtil.isNull(goodsId)){
            result.setFalse("不能为空");
            return result;
        }
        emgPlanPublicGoodsMapper.deleteByPrimaryKey(goodsId);
        return result;
    }

    public Result find(EmgPlanPublicGoods emgPlanPublicGoods, Page page){
        Result result=new Result();
        PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
        try {
            List<EmgPlanPublicGoods> emgPlanPublicGoodsList
                    =emgPlanPublicGoodsMapper.findByPlanPublicId(emgPlanPublicGoods.getPlanPublicId());
            result.setData(new PageInfo<>(emgPlanPublicGoodsList));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
